package com.lb.shopguide.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;


import com.lb.shopguide.config.AppConfigManager;
import com.lb.shopguide.constants.AppConstant;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/8/11
 *     desc  : 文件相关工具类扩展
 * </pre>
 */
public final class FileUtilsExt {


    /**
     * 将得到的一个Bitmap保存到SD卡上，得到一个绝对路径
     */
    public static String saveBitmap(Bitmap bm) {
        //在SD卡上创建目录
        String destinationFileName = AppConstant.SAMPLE_CROPPED_IMAGE_NAME + AppConstant.PNG;
        File img = new File(AppConfigManager.DIR_IMAGE, destinationFileName);
        try {
            if (img.exists())
                img.delete();
            img.createNewFile();
            FileOutputStream fos = new FileOutputStream(img);
            bm.compress(Bitmap.CompressFormat.PNG, 85, fos);
            fos.flush();
            fos.close();
            return img.getCanonicalPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String saveImageToGallery(Context context, String name, Bitmap bmp) {         // 首先保存图片
        if (bmp == null) {
            return null;
        }
        String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath()+"/lb" : "/mnt/sdcard/lb";
        File dir = new File(SAVE_PIC_PATH);
        File file = new File(SAVE_PIC_PATH, name);
        try {
            if(!dir.exists()){
                dir.mkdirs();
            }
            if (file.exists()) file.delete();
            file.createNewFile();///storage/emulated/0/Android/data/com.lb.shopguide/files/image/_android_3a95fa6576fe2398b9ba6858.png
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 85, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }         // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), name, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } // 最后通知图库更新 //

//        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        return file.getAbsolutePath();
    }
}